package com.study4j.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.alibaba.fastjson.JSON;

/**   
 * @description  字符串工具类
 * @author jiangbin   
 * @date 2018年10月23日 下午2:17:10 
 * @version 1.0.0  
 */
public class StringUtil {
	private static Pattern p = Pattern.compile("\\s*|\t|\r|\n");
	
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}

		return dest;
	}

	public static boolean isNotEmpty(String str) {
		return str != null && str.length() > 0;
	}

	public static boolean isEmpty(String str) {
		return !isNotEmpty(str);
	}

	public static boolean isNotEmpty(Object str) {
		boolean flag = true;
		if (str != null && !str.equals("")) {
			if (str.toString().length() > 0) {
				flag = true;
			}
		} else {
			flag = false;
		}

		return flag;
	}

	public static String toString(Object obj) {
		if (obj != null) {
			Class objClazz = obj.getClass();
			StringBuilder sb = new StringBuilder();
			sb.append(objClazz).append(":");
			sb.append(JSON.toJSONString(obj));
			return sb.toString();
		} else {
			return "null";
		}
	}

	public static String getStackTrace(Exception e) {
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		e.printStackTrace(new PrintWriter(buf, true));
		String expMessage = buf.toString();

		try {
			buf.close();
			return expMessage;
		} catch (IOException arg3) {
			return e.getMessage();
		}
	}

	public static String getStackTrace(Exception e, int length) {
		String stackTrace = getStackTrace(e);
		return stackTrace != null && stackTrace.length() > length ? stackTrace.substring(0, length) : stackTrace;
	}

	/** 
	 * @description 判断字符串是否由正整数组成
	 * @param str 需要判断的字符串
	 * @return 判断结果
	 */
	public static boolean isPositiveInteger(String str) {
		return str.matches("[1-9]+\\d*");
	}
	
	/** 
	 * @description 判断字符串是否由数字组成
	 * @param str 需要判断的字符串
	 * @return 判断结果
	 */
	public static boolean isNumeric(String str) {
		return str.matches("-?[0-9]+.?[0-9]*");
	}

	public static boolean isNumeric1(String str) {
		return str.matches("([0-9]{0,6})|([0]\\.\\d{1,2}|[0-9]{0,6}\\.\\d{1,2})");
	}


	public static String stringFilter(String str, String flag) throws PatternSyntaxException {
		String regEx = "[`~!@#$%^&*()+=|{}\':;\',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll(flag).trim();
	}

	public static int getTimeFrom(String baidu) {
		int res = 0;
		int indexOf = baidu.indexOf("天");
		if (indexOf >= 0) {
			String indexOf2 = baidu.substring(0, indexOf);
			res = Integer.valueOf(indexOf2).intValue() * 24 * 60;
			baidu = baidu.substring(indexOf + 1);
		}

		int indexOf21 = baidu.indexOf("小时");
		if (indexOf21 >= 0) {
			if (indexOf < 0) {
				boolean indexOf1 = false;
			}

			String indexOf3 = baidu.substring(0, indexOf21);
			res += Integer.valueOf(indexOf3).intValue() * 60;
			baidu = baidu.substring(indexOf21 + 2);
		}

		int indexOf31 = baidu.indexOf("分钟");
		if (indexOf31 >= 0) {
			if (indexOf21 < 0) {
				boolean indexOf22 = false;
			}

			String t = baidu.substring(0, indexOf31);
			res += Integer.valueOf(t).intValue();
		}

		return res;
	}

	public static String formateSeq(String str, int length) {
		String strLenth = "0";
		for (int seqNo = 1; seqNo < length; ++seqNo) {
			strLenth = strLenth + "0";
		}

		Integer arg5 = Integer.valueOf(Integer.parseInt(str));
		arg5 = Integer.valueOf(arg5.intValue() + 1);
		DecimalFormat df = new DecimalFormat(strLenth);
		return df.format(arg5);
	}

}
